package com.gscitysfy.cus.drain.modules.resource.convert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.portal.feign.DistrictService;
import com.gscitysfy.cus.drain.modules.command.entity.DangerSituationManage;
import com.gscitysfy.cus.drain.modules.command.entity.InstructCar;
import com.gscitysfy.cus.drain.modules.command.entity.InstructWarehouse;
import com.gscitysfy.cus.drain.modules.command.mapper.InstructCarMapper;
import com.gscitysfy.cus.drain.modules.command.mapper.InstructWarehouseMapper;
import com.gscitysfy.cus.drain.modules.resource.dto.DrainBaseMnWarehouseDto;
import com.gscitysfy.cus.drain.modules.resource.dto.ResourceCarDto;
import com.gscitysfy.cus.drain.modules.resource.entity.DrainBaseMnWarehouse;
import com.gscitysfy.cus.drain.modules.resource.entity.ResourceCar;
import com.gscitysfy.cus.drain.utils.CoordinateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author jiang_wen
 * @Date 2022/10/23 15:42
 * @Description TODO
 */
@AllArgsConstructor
@Component
public class ResourceWarehouseConvert {

    private final DistrictService districtService;

    private final InstructWarehouseMapper instructWarehouseMapper;

    public DrainBaseMnWarehouse toEntity(DrainBaseMnWarehouse drainBaseMnWarehouse, DrainBaseMnWarehouseDto drainBaseMnWarehouseDto) {
        drainBaseMnWarehouse.setAddress(drainBaseMnWarehouseDto.getAddress());
        drainBaseMnWarehouse.setWarehouseName(drainBaseMnWarehouseDto.getWarehouseName());
        drainBaseMnWarehouse.setPhoneNo(drainBaseMnWarehouseDto.getPhoneNo());
        drainBaseMnWarehouse.setLgtd(drainBaseMnWarehouseDto.getLgtd());
        drainBaseMnWarehouse.setLttd(drainBaseMnWarehouseDto.getLttd());
        drainBaseMnWarehouse.setPersoner(drainBaseMnWarehouseDto.getPersoner());
        drainBaseMnWarehouse.setRemark(drainBaseMnWarehouseDto.getRemark());
        drainBaseMnWarehouse.setCmRegflag(drainBaseMnWarehouseDto.getCmRegflag());
        return drainBaseMnWarehouse;
    }

    public DrainBaseMnWarehouseDto toDto(DrainBaseMnWarehouse drainBaseMnWarehouse, DangerSituationManage dangerSituationManage) {
        DrainBaseMnWarehouseDto drainBaseMnWarehouseDto = new DrainBaseMnWarehouseDto();
        drainBaseMnWarehouseDto.setCmKid(drainBaseMnWarehouse.getCmKid());
        drainBaseMnWarehouseDto.setWarehouseName(drainBaseMnWarehouse.getWarehouseName());
        drainBaseMnWarehouseDto.setPhoneNo(drainBaseMnWarehouse.getPhoneNo());

        //计算经纬度
        if(drainBaseMnWarehouse.getLgtd()!=null&&drainBaseMnWarehouse.getLttd()!=null&&dangerSituationManage.getLongitude()!=null&&dangerSituationManage.getLatitude()!=null){
            BigDecimal startXPoint = drainBaseMnWarehouse.getLgtd();
            BigDecimal startYPoint = drainBaseMnWarehouse.getLttd();
            BigDecimal endXPoint = dangerSituationManage.getLongitude();
            BigDecimal endYPoint = dangerSituationManage.getLatitude();

            BigDecimal distance = CoordinateUtil.getDistance(startXPoint, startYPoint, endXPoint, endYPoint);
            drainBaseMnWarehouseDto.setDistance(distance.doubleValue());
        }


        LambdaQueryWrapper<InstructWarehouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(InstructWarehouse::getWarehouseId, drainBaseMnWarehouse.getCmKid());
        queryWrapper.eq(InstructWarehouse::getStatus, "ACCEPT");
        queryWrapper.eq(InstructWarehouse::getCmUsable, false);
        List<InstructWarehouse> instructWarehouseList = instructWarehouseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(instructWarehouseList)) {
            drainBaseMnWarehouseDto.setAssignedFlag(true);
        }else {
            drainBaseMnWarehouseDto.setAssignedFlag(false);
        }
        return drainBaseMnWarehouseDto;
    }


    public DrainBaseMnWarehouseDto toDto(DrainBaseMnWarehouse drainBaseMnWarehouse) {
        DrainBaseMnWarehouseDto drainBaseMnWarehouseDto = new DrainBaseMnWarehouseDto();

        drainBaseMnWarehouseDto.setAddress(drainBaseMnWarehouse.getAddress());
        drainBaseMnWarehouseDto.setWarehouseName(drainBaseMnWarehouse.getWarehouseName());
        drainBaseMnWarehouseDto.setPhoneNo(drainBaseMnWarehouse.getPhoneNo());
        drainBaseMnWarehouseDto.setLgtd(drainBaseMnWarehouse.getLgtd());
        drainBaseMnWarehouseDto.setLttd(drainBaseMnWarehouse.getLttd());
        drainBaseMnWarehouseDto.setPersoner(drainBaseMnWarehouse.getPersoner());
        drainBaseMnWarehouseDto.setRemark(drainBaseMnWarehouse.getRemark());
        drainBaseMnWarehouseDto.setCmRegflag(drainBaseMnWarehouse.getCmRegflag());
        drainBaseMnWarehouseDto.setCmKid(drainBaseMnWarehouse.getCmKid());

        if (Objects.nonNull(drainBaseMnWarehouse.getCmRegflag())) {
            Result<String> result = districtService.getDistrictNameByCode(String.valueOf(drainBaseMnWarehouse.getCmRegflag()));
            if (result.getStatus() == 200) {
                drainBaseMnWarehouseDto.setCmRegflagName(result.getData());
            }
        }
        drainBaseMnWarehouseDto.setAssignedFlag(drainBaseMnWarehouse.getAssignedFlag());
        return drainBaseMnWarehouseDto;
    }

    public List<DrainBaseMnWarehouseDto> toDtoList(List<DrainBaseMnWarehouse> drainBaseMnWarehouseList) {
        if (CollectionUtils.isEmpty(drainBaseMnWarehouseList)) {
            return Collections.emptyList();
        }
        return drainBaseMnWarehouseList.stream().map(this::toDto).collect(Collectors.toList());
    }

}
